/******************************************************************
 *  MRRM's instruction fetch unit                                 *
 *                                                                *
 *  This file is part of the MRRM project                         *
 *  <http://mrrm.googlecode.com/>                                 *
 *                                                                *
 *  Author(s):                                                    *
 *    -  Yuan Pengfei <coolypf@opencores.org>                     *
 *                                                                *
 ******************************************************************
 *                                                                *
 *  Copyright (C) 2010 AUTHORS                                    *
 *                                                                *
 *  This source file may be used and distributed without          *
 *  restriction provided that this copyright statement is not     *
 *  removed from the file and that any derivative work contains   *
 *  the original copyright notice and the associated disclaimer.  *
 *                                                                *
 *  MRRM is free software: you can redistribute it and/or modify  *
 *  it under the terms of the GNU General Public License as       *
 *  published by the Free Software Foundation, either version 3   *
 *  of the License, or (at your option) any later version.        *
 *                                                                *
 *  MRRM is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of    *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  *
 *  GNU General Public License for more details.                  *
 *                                                                *
 *  You should have received a copy of the GNU General Public     *
 *  License along with MRRM. If not, see                          *
 *  <http://www.gnu.org/licenses/>.                               *
 *                                                                *
 ******************************************************************/

`include "global.v"

module mrrm_if(
	// Instruction memory interface
	imemaddr, imemdata,
	// IF interface
	F_predpc, f_insn, f_valp, f_predpc,
	// Halt signal
	halt,
	// EXE interface
	E_itype, E_isubtype, E_ifun, E_valb,
	// MEM interface
	M_bch, M_itype, M_valp
	);

parameter iw = `INSTRUCTION_WIDTH;
parameter aw = `MEMORY_ADDR_WIDTH;
	
// Interfaces
// Memory interface
output [aw-1:0] imemaddr;
input [iw-1:0] imemdata;
// IF interface
input [aw-1:0] F_predpc;
output [iw-1:0] f_insn;
output [aw-1:0] f_valp;
output [aw-1:0] f_predpc;
// Halt signal
output halt;
// EXE interface
input [2:0] E_itype;
input [1:0] E_isubtype;
input [3:0] E_ifun;
input [aw-1:0] E_valb;
// MEM interface
input M_bch;
input [2:0] M_itype;
input [aw-1:0] M_valp;

// Fetch instruction
assign imemaddr = (M_itype == `I_BXX && M_bch == 1'b0) ? M_valp 
	: ({E_itype, E_isubtype, E_ifun} == {`I_REG, `S_TWO, `F_JUMP}) ? E_valb : F_predpc;
assign f_insn = imemdata;

// New PC
assign f_valp = imemaddr + 32'd4;
assign f_predpc = (f_insn[31:29] == `I_BXX) ?
	(f_valp + {{6{f_insn[23]}}, f_insn[23:0], 2'b0}) : f_valp;
	
// Halt signal
assign halt = (imemdata[31:29] == `I_HLT) ? 1'b1 : 1'b0;

endmodule
